package skills.jssvc.deu.cn.skillsecond.programming3;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import skills.jssvc.deu.cn.skillsecond.MyApplication;
import skills.jssvc.deu.cn.skillsecond.R;
import skills.jssvc.deu.cn.skillsecond.adapter.BillMangAdapter;
import skills.jssvc.deu.cn.skillsecond.entry.BillMang;
import skills.jssvc.deu.cn.skillsecond.util.SqlHelper;

public class BillMangFragment extends Fragment {
    private final String TAG = "BillMangFragment";
    private View view;
    private Spinner spinner;
    private Button queryBtn;
    private RecyclerView recyclerView;
    private LinearLayoutManager manager;
    private List<BillMang> billList;
    private BillMangAdapter adapter;

    private SqlHelper helper;
    private SQLiteDatabase db;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bill_mang, container, false);
        initView();
        initData();
        setListener();
        return view;
    }


    private void initView(){
        spinner = (Spinner) view.findViewById(R.id.frag3_billmang_Spinner);
        queryBtn = (Button) view.findViewById(R.id.frag3_billmang_query_btn);
        recyclerView = (RecyclerView) view.findViewById(R.id.frag3_billrecord_recyclerview);
        manager = new LinearLayoutManager(MyApplication.appContext);
        recyclerView.setLayoutManager(manager);
        billList = new ArrayList<>();
        adapter = new BillMangAdapter(billList);
        recyclerView.setAdapter(adapter);
    }

    private void initData(){
        helper = new SqlHelper(MyApplication.appContext, SqlHelper.DA_NAME, null, 1);
        db = helper.getWritableDatabase();
        Cursor cursor = db.query(SqlHelper.table_top, null, null, null, null, null, null);
        if(cursor.moveToFirst()){
            do{
                billList.add(new BillMang(cursor.getInt(cursor.getColumnIndex("id")),
                        cursor.getInt(cursor.getColumnIndex("carId")),
                        cursor.getInt(cursor.getColumnIndex("amount")),
                        cursor.getString(cursor.getColumnIndex("user")),
                        cursor.getString(cursor.getColumnIndex("time"))));
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        adapter.notifyDataSetChanged();
    }

    private void setListener(){
        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sort(spinner.getSelectedItemPosition());
            }
        });
    }
    private void sort(int type){
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
        switch (type) {
            case 0:
                Collections.sort(billList, new Comparator<BillMang>() {
                    @Override
                    public int compare(BillMang billMang, BillMang t1) {
                        Date max = format.parse(billMang.getTime(), new ParsePosition(0));
                        Date min = format.parse(t1.getTime(), new ParsePosition(0));
                        return billMang.getTime().compareTo(t1.getTime());
                    }
                });
                break;
            case 1:
                Collections.sort(billList, new Comparator<BillMang>() {
                    @Override
                    public int compare(BillMang billMang, BillMang t1) {
                        Date max = format.parse(billMang.getTime(), new ParsePosition(0));
                        Date min = format.parse(t1.getTime(), new ParsePosition(0));
                        return t1.getTime().compareTo(billMang.getTime());
                    }
                });
                break;
            default:break;
        }
    }
}
